package cn.wubo.redis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    @RequestMapping(value = "/set", method = RequestMethod.POST)
    public void set(String key, String value) {
        this.set(key, value, null);
    }

    /**
     * 写入缓存并设置时效时间
     *
     * @param key
     * @param value
     * @param expireTime
     * @return
     */
    @RequestMapping(value = "/setWithExpireTime", method = RequestMethod.POST)
    public void set(String key, String value, Long expireTime) {
        stringRedisTemplate.opsForValue().set(key, value);
        if (expireTime != null) {
            stringRedisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
        }
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    public void remove(String key) {
        if (stringRedisTemplate.hasKey(key)) {
            stringRedisTemplate.delete(key);
        }
    }

    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    @RequestMapping(value = "/removeByKeys", method = RequestMethod.DELETE)
    public void removeKeys(String... keys) {
        stringRedisTemplate.delete(Arrays.asList(keys));
    }

    /**
     * 批量删除模糊查询出的key
     *
     * @param pattern
     */
    @RequestMapping(value = "/removeByPattern", method = RequestMethod.DELETE)
    public void removePattern(String pattern) {
        Set<String> keys = stringRedisTemplate.keys(pattern);
        if (keys.size() > 0)
            stringRedisTemplate.delete(keys);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 哈希 添加
     *
     * @param key
     * @param hashKey
     * @param value
     */
    @RequestMapping(value = "/hmSet", method = RequestMethod.POST)
    public void hmSet(String key, Object hashKey, Object value) {
        stringRedisTemplate.opsForHash().put(key, hashKey, value);
    }

    /**
     * 哈希获取数据
     *
     * @param key
     * @param hashKey
     * @return
     */
    @RequestMapping(value = "/hmGet", method = RequestMethod.GET)
    public Object hmGet(String key, Object hashKey) {
        return stringRedisTemplate.opsForHash().get(key, hashKey);
    }

    /**
     * 列表添加
     *
     * @param k
     * @param v
     */
    @RequestMapping(value = "/lPush", method = RequestMethod.POST)
    public void lPush(String k, String v) {
        stringRedisTemplate.opsForList().rightPush(k, v);
    }

    /**
     * 列表获取
     *
     * @param k
     * @param l
     * @param l1
     * @return
     */
    @RequestMapping(value = "/lRange", method = RequestMethod.GET)
    public List<String> lRange(String k, long l, long l1) {
        return stringRedisTemplate.opsForList().range(k, l, l1);
    }

    /**
     * 集合添加
     *
     * @param key
     * @param values
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void add(String key, String... values) {
        stringRedisTemplate.opsForSet().add(key, values);
    }

    /**
     * 集合获取
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "/members", method = RequestMethod.GET)
    public Set<String> members(String key) {
        return stringRedisTemplate.opsForSet().members(key);
    }

    /**
     * 有序集合添加
     *
     * @param key
     * @param value
     * @param scoure
     */
    @RequestMapping(value = "/zAdd", method = RequestMethod.POST)
    public void zAdd(String key, String value, double scoure) {
        stringRedisTemplate.opsForZSet().add(key, value, scoure);
    }

    /**
     * 有序集合获取
     *
     * @param key
     * @param scoure
     * @param scoure1
     * @return
     */
    @RequestMapping(value = "/rangeByScore", method = RequestMethod.GET)
    public Set<String> rangeByScore(String key, double scoure, double scoure1) {
        return stringRedisTemplate.opsForZSet().rangeByScore(key, scoure, scoure1);
    }
}
